<?php
/*------------------------------------------------------------------------------ 
    
    Create Date :2010-08-13
    Author    : Phan Van Dam
    Copyright(c) 2010 A.D.A Solution. All rights reserved
    
    ------------------------------------------------------------------------------
    Update History:
    Ver.    TRB#            Date          Author       Note
    3.0     thiáº¿u comment  2011/03/10     HoangPhuc     Review code,update comment for source
------------------------------------------------------------------------------*/

if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}
$objXajax->registerFunction('cusfrmproduct_loadGrid');      
$objXajax->registerFunction('cusfrmproduct_save');
$objXajax->registerFunction('cusfrmproduct_edit');
//$objXajax->registerFunction('cusfrmproduct_delete');
$objXajax->registerFunction('cusfrmproduct_fillMenuParent');
$objXajax->registerFunction('cusfrmproduct_init');

$objXajax->registerFunction('cusfrmproduct_dfloadproduct');
$objXajax->registerFunction('cusfrmproduct_dfaddProduct');


function cusfrmproduct_init()
{
    global $objDbSelect,$user_id,$tbl_postfix,$lang;
    $objResponse =  new xajaxResponse();
    $name = 'name_vi';
    $strCbb=createCombobox('cus_unit',array('id',$name),array('name'=>$name,'value'=>'id'),'cusfrmproduct_cbbunit',false) ; 
     $objResponse->addAssign('cusfrmproduct_tdUnit', 'innerHTML',$strCbb);
    $objResponse->addScript('changeWaiting(1);');
    return $objResponse->getXML();
}

function cusfrmproduct_save($params)
{
    global $objDbSelect,$user_id,$tbl_postfix;
    $objResponse =  new xajaxResponse();  
    $name_vi = strip_tags($params['txtnamemenuVi']); 
    $parent_id = $params['cbbMenuParent'];
    $price = $params['txtprice'];
    $pricebuy = $params['txtpricebuy'];
    $unit = $params['cbbunit'];
    $area_ext = $params['chkareaext'];
    $chkquanlity = $params['chkquanlity'];
    
    $edit = $params['hdEdit'];
    $id = $params['hdId'];
    $sql = ""; 
    if($edit==1){
        $sql ="UPDATE cus_product$tbl_postfix SET name_vi = '$name_vi',name_en = '$name_vi',parent_id = '$parent_id',
            price = '$price',unit = '$unit',checkquanlity='$chkquanlity',area_ext='$area_ext',
            pricebuy='$pricebuy', user_update = '$user_id',date_update = now() WHERE id =$id"; 
        //$objDbSelect->Execute($sql);
        
    }else{
          $sql= " INSERT INTO cus_product$tbl_postfix(name_vi,name_en,active,unit,price,parent_id,user_created,
          date_created,user_update,date_update,checkquanlity,pricebuy,area_ext) VALUES('$name_vi','$name_vi',1,
          '$unit','$price','$parent_id','$user_id',now(),'$user_id',now(),'$chkquanlity','$pricebuy','$area_ext')"; 
          
          //$objDbSelect->Execute($sql); 
     }
     
     if($objDbSelect->Execute($sql)){
       $objResponse->addScript("checkUpdateStatus(1,'cusfrmproduct','$edit');");
       $objResponse->addScript('xajax_cusfrmproduct_loadGrid();'); 
        }else{
       $objResponse->addScript("checkUpdateStatus(0,'cusfrmproduct','$edit');");
    }
     $objResponse->addScript("hasChange= true;");   
/*    $objResponse->addScript('changeWaiting(1);');
    $objResponse->addScript('xajax_cusfrmproduct_loadGrid();'); */    
    return $objResponse->getXML();
}

function cusfrmproduct_fillMenuParent()
{
    global $objDbSelect,$lang,$tbl_postfix;
    $objResponse =  new xajaxResponse();
    
    $sql = " SELECT id, name_vi as name FROM cus_product$tbl_postfix WHERE 
       product_type = 0 ";   
    $arr = $objDbSelect->GetArray($sql);
    $count = count($arr);
    $temp ='';
    for($i=0;$i<$count;$i++){
        $id = $arr[$i]['id'];
        $name = $arr[$i]['name'];
        $temp.= "<option value='$id'> $name </option>"  ;
    }
    $tempdf =  '<select id="cusfrmproduct_cbbMenuParentDefault" class="cbbRequired"><option value="">--Chọn--</option>'.$temp.'</select>';
    $temp =  '<select id="cusfrmproduct_cbbMenuParent" class="cbbRequired"><option value="">--Chọn--</option>'.$temp.'</select>';
    $objResponse->addAssign('cusfrmproduct_tdmenuparent', 'innerHTML',$temp);    
    $objResponse->addAssign('cusfrmproduct_dftdmenuparent', 'innerHTML',$tempdf);
    return $objResponse->getXML();
}
function cusfrmproduct_loadGrid()
{ 
    global $lang,$path;
    $objResponse =  new xajaxResponse();// dong luon luon co khi goi ham ajax
    $arrGrid = cusfrmproduct_getGrid(); 
    //echo '<pre>'; print_r($arrGrid); die();   
    $oTbs =  new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/cus_product_grid.html');
    $oTbs->MergeBlock('blk_data', $arrGrid);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('cusfrmproduct_divGrid', 'innerHTML', $oTbs->Source);
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('cusfrmproduct_tbl');");     
    $objResponse->addScript('changeWaiting(1);');                        
    return $objResponse->getXML(); // dong luon luon co khi goi ham ajax   
}
function cusfrmproduct_getGrid()
{
    global $objDbSelect,$lang,$tbl_postfix;
    $sqlSelect = " select pt1.id,pt1.active, pt1.name_vi,pt1.name_en,pt2.name_vi as parent_name,pt1.price as price, 
                   pt1.unit as unit,pt1.icon as icon ,pt1.id_default, pt1.checkquanlity ,pt1.showmenu,pt1.pricebuy,cu.name_vi as unitname,pt1.area_ext
                    from cus_product$tbl_postfix as pt1 INNER JOIN  cus_product$tbl_postfix as pt2 ON 
                   pt1.parent_id = pt2.id left join cus_unit cu ON pt1.unit = cu.id  WHERE  pt1.product_type =1  order by pt1.parent_id,pt1.name_vi ";  
                                 
    return $objDbSelect->GetArray($sqlSelect);
}  

    function cusfrmproduct_edit($id){
        global $objDbSelect,$tbl_postfix;
        $objResponse =  new xajaxResponse();
        $sql = " SELECT * FROM cus_product$tbl_postfix WHERE id = '$id' ";
        $arr = $objDbSelect->GetArray($sql);
        $name_vi        = $arr[0]['name_vi'];          
        $parent_id     = $arr[0]['parent_id'];
        $price         =FormatMoney($arr[0]['price']) ;       
        $pricebuy         = FormatMoney($arr[0]['pricebuy']);
        $checkquanlity         = $arr[0]['checkquanlity'];
        $unit         = $arr[0]['unit'];
        $area_ext         = $arr[0]['area_ext'];
        
        $objResponse->addScript("$('#cusfrmproduct_txtnamemenuVi').val('$name_vi');");
        $objResponse->addScript("$('#cusfrmproduct_cbbMenuParent').val('$parent_id');");
        $objResponse->addScript("$('#cusfrmproduct_txtprice').val('$price');");
        $objResponse->addScript("$('#cusfrmproduct_txtpricebuy').val('$pricebuy');");
        $objResponse->addScript("$('#cusfrmproduct_cbbunit').val('$unit');");

        if($checkquanlity==1){
           $objResponse->addScript("$('#cusfrmproduct_chkquanlity').attr('checked','checked');"); 
        }else{
           $objResponse->addScript("$('#cusfrmproduct_chkquanlity').attr('checked',false);");   
        }       
         if($area_ext==1){
           $objResponse->addScript("$('#cusfrmproduct_chkareaext').attr('checked','checked');"); 
        }else{
           $objResponse->addScript("$('#cusfrmproduct_chkareaext').attr('checked',false);");   
        }
        
        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
    function cusfrmproduct_dfloadproduct(){
        global $objDbSelect,$tbl_postfix,$lang,$path;
        $objResponse =  new xajaxResponse();
        $arrGrid = cusfrmproduct_dfgetGrid();
        if(count($arrGrid)<1){
          $objResponse->addAlert('Không còn sản phẩm mặc định của hệ thống');
          return $objResponse->getXML();  
        }
        $oTbs =  new clsTinyButStrong();
        $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/cus_product_default_grid.html');
        $oTbs->MergeBlock('blk_data', $arrGrid);
        $oTbs->Show(TBS_NOTHING);
        $objResponse->addAssign('cusfrmproduct_dftdproduct', 'innerHTML', $oTbs->Source);
        //$objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('cusfrmproduct_tbl');");     
        $objResponse->addScript("$('#cusfrmproduct_divAddProduct').dialog('open');");     
        $objResponse->addScript("initForm('cusfrmproductdefault_tbl');");     

        $objResponse->addScript('changeWaiting(1);');
        return $objResponse->getXML();   
    }
    
    function cusfrmproduct_dfgetGrid()
    {
        global $objDbSelect,$lang,$tbl_postfix,$hotel_id;
        $name = 'name_vi' ;
        if($lang!='vi'){
         $name = 'name_en';   
        }
        $sqlSelect = " select pd1.$name as name,pd1.checkquanlity,pd1.id,pd2.$name as nameparent from cus_product_default as pd1 
        join cus_product_default as pd2 ON pd1.parent_id = pd2.id WHERE  pd1.active = 1 and pd1.product_type = 1 and pd1.id not in 
        (select id_default from cus_product_$hotel_id) order by pd1.parent_id, pd1.$name ";                        
        return $objDbSelect->GetArray($sqlSelect);
    }     
    function cusfrmproduct_dfaddProduct($params)
    {
        global $objDbSelect,$lang,$tbl_postfix,$hotel_id,$lang,$user_id;
        $objResponse =  new xajaxResponse(); 
            $producttype = $params['producttype'] ;
            $strid      = substr($params['strid'],1);
            $strprice   = substr($params['strprice'],1); 
            $strChk     = substr($params['strChk'],1);
            $strpricebuy     = substr($params['strpricebuy'],1);
           
            $arrId = explode(',',$strid); 
            $arrprice = explode(',',$strprice); 
            $arrChk = explode(',',$strChk); 
            $arrpricebuy = explode(',',$strpricebuy);
            $arrDetail = array();
            for($i=0;$i<count($arrId);$i++){
                $arrDetail[$arrId[$i]]['price']= $arrprice[$i];
                $arrDetail[$arrId[$i]]['chk']= $arrChk[$i];
                $arrDetail[$arrId[$i]]['pricebuy']= $arrpricebuy[$i];
            } 
             
            
            $sql = "select id, name_vi,name_en,unit from cus_product_default WHERE id in ($strid)"; 
            $arr = $objDbSelect->GetArray($sql);
            $count= count($arr);
            $values ='';
            for($i=0;$i<$count;$i++){
                $name_vi = $arr[$i]['name_vi']; 
                $name_en = $arr[$i]['name_en']; 
                $unit = $arr[$i]['unit']; 
                $id = $arr[$i]['id'];
                $price = $arrDetail[$id]['price'];
                $pricebuy = $arrDetail[$id]['pricebuy'];
                $chkquanlity = $arrDetail[$id]['chk'];
              $values .=",('$name_vi','$name_en',1,'$unit','$price','$producttype','$user_id',now(),'$user_id',now(),'$chkquanlity','$pricebuy','$id')";  
            }
            $value = substr($values,1);
            
            $sqlInsert = " INSERT INTO cus_product$tbl_postfix(name_vi,name_en,active,unit,price,parent_id,user_created,
          date_created,user_update,date_update,checkquanlity,pricebuy,id_default) values$value";

          $objDbSelect->Execute($sqlInsert);
          $objResponse->addScript("$('#cusfrmproduct_divAddProduct').dialog('close');");
          $objResponse->addScript('xajax_cusfrmproduct_loadGrid();');   
        return $objResponse->getXML();
    } 
        
?>
